<template>
	<view>
		<view v-if="notLogin" class="notLogin-mask" @click="reLogin"></view>
		<view class="u-page"> 
		<!-- v-if="token!=''" -->
			<block v-if="state.tabValue === 0">
				<home @open-signIn="openSignIn" @open-login="openLogin"></home>
			</block>
			<block v-if="state.tabValue === 1">
				<mine @open-signIn="openSignIn"></mine>
			</block>
		</view>
		<!-- 底部bar -->
		<up-tabbar :value="state.tabValue" @change="changeTab" :fixed="true" placeholder :safeAreaInsetBottom="true" activeColor="#f15e69">
			<up-tabbar-item text="幸运购">
				<template #active-icon>
					<image class="u-page__item__slot-icon" src="@/static/tabarIcon/tab-1h.png"></image>
				</template>
				<template #inactive-icon>
					<image class="u-page__item__slot-icon" src="@/static/tabarIcon/tab-1.png"></image>
				</template>
			</up-tabbar-item>
			<!-- <up-tabbar-item text="发起抽奖">
				<template #active-icon>
					<image class="u-page__item__slot-icon" src="@/static/tabarIcon/tab-2h.png"></image>
				</template>
				<template #inactive-icon>
					<image class="u-page__item__slot-icon" src="@/static/tabarIcon/tab-2.png"></image>
				</template>
			</up-tabbar-item> -->
			<up-tabbar-item text="我的">
				<template #active-icon>
					<image class="u-page__item__slot-icon" src="@/static/tabarIcon/tab-3h.png"></image>
				</template>
				<template #inactive-icon>
					<image class="u-page__item__slot-icon" src="@/static/tabarIcon/tab-3.png"></image>
				</template>
			</up-tabbar-item>
		</up-tabbar>
		<signIn v-if="token&&!notLogin" :signInShow="signInVisible" @close="handleCloseSign"></signIn>
		<loginPop :loginShow="loginVisible" @close="handleCloseSign"></loginPop>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow,
		onShareAppMessage
	} from '@dcloudio/uni-app'
	import {
		onMounted,
		reactive,
		nextTick,
		computed,
		ref
	} from 'vue';
	import {
		useAuth
	} from '/composables/useAuth';
	import {
		useCommon
	} from '/composables/useCommon.js';
	import {
		useStore
	} from 'vuex';
	// 计算属性，用于获取 Vuex 中的状态
	const {
		appid,
		qnUpToken,
		qiniuDomain,
		systemInfo,
		statusHeight,
		navHeight,
		tabbarIndex,
		tabbarShow,
		tabbarHeight,
		bottomSafeHeight,
		isAndroid,
		getQnUpToken,
		reloadAllSystemInfo
	} = useCommon();
	const {
		token,
		user_id,
		userInfo,
		hasAuth,
		session_key,
		SET_TOKEN,
		autoLogin,
		wxGetUserInfo,
		logout,
		refreshAppId,
		login,
		hasSigned,
		getUserAccount,
		notLogin,
		REMOVE_TOKEN
	} = useAuth();
	
	const store = useStore();
	// 签到弹窗
	const signInVisible = ref(false);
	const loginVisible = ref(false);
	const state = reactive({
		tabValue:0,
		luckyShareInfo:{}
	})
	onLoad(async (options) => {
	    try {
			//是否有邀请人id
			// state.luckyShareInfo = uni.getStorageSync('luckyShareInfo')||{};
			const inviterId = options.inviterId||''
			await login(inviterId); // 只需调用一次 login 方法
			if(!notLogin.value){
				getUserAccount()
			}
		} catch (e) {
			console.error('登录流程失败', e);
		}
	});
	// 关键：新增 reLogin 方法，按照你的需求先移除 token 再重新登录
	async function reLogin() {
		try {
			uni.showToast({
				icon:'none',
				title:'需要重新登录'
			})
			await REMOVE_TOKEN(); // 先清除当前 token
			await login();       // 再执行重新登录
			getUserAccount();    // 登录成功后，获取用户账户信息
		} catch (error) {
			console.error('重新登录失败', error);
		}
	}
	// 接收 home 发出的显示签到事件
	function openSignIn() {
		signInVisible.value = true;
	}
	function openLogin() {
		loginVisible.value = true;
	}
	// 监听子组件的关闭签到事件
	function handleCloseSign() {
		signInVisible.value = false;
		loginVisible.value = false;
	}
	// 定义分享内容
	onShareAppMessage(() => {
		const luckyShareInfo = uni.getStorageSync('luckyShareInfo') || {};
		return {
			title: luckyShareInfo.name&&state.tabValue===0?'免费参加！' + luckyShareInfo.name:'送你免费抽奖机会，点我中奖率翻倍，快来看看吧！',
			path: '/pages/index/index?inviterId='+userInfo.value.id,
			// desc:'剩余抽奖号码：'+luckyShareInfo.remnant+'个',
			imageUrl: luckyShareInfo.name&&state.tabValue===0?luckyShareInfo.cover:'/static/share-index.png',
			success: function (res) {
				uni.showToast({ title: '分享成功' })
				console.log('分享成功', res)
			},
			fail: function (err) {
				uni.showToast({ title: '分享失败', icon: 'none' })
				console.log('分享失败', err)
			}
		}
	})
	onMounted(async () => {
		state.pageLength = getCurrentPages().length;
		try {
			// const accountInfo = await wx.getAccountInfoSync();
			// store.commit('common/SET_APPID', accountInfo.miniProgram.appId);
			// const systemInfo = await wx.getSystemInfoSync();
			// store.commit('common/SET_SYSTEM_INFO', systemInfo);
			// if (systemInfo) {
			// 	store.commit('common/SET_NAV_SIZE');
			// }
			// if (/android/i.test(systemInfo.system)) {
			// 	store.commit('common/SET_IS_ANDROID', true);
			// }
			// store.commit('common/SET_TABBAR_INDEX', 1);
			// store.commit('common/SET_TABBAR_SHOW', true);
		} catch (error) {
			console.error('Error during onMounted:', error);
		}
	});
	async function changeTab(e) {
		await login(); // Tab 切换时也调用封装的登录方法
		state.tabValue = e;
	}
</script>

<style lang="scss" scoped>
	::v-deep{
		.u-tabbar__content__item-wrapper,.u-safe-bottom{
			background: #fafafa;
		}
	}
	.u-page__item__slot-icon{
		width: 44rpx;
		height: 44rpx;
	}
	
</style>
